/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scacp;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author cleber
 */
public class ProvaSalvarComo extends javax.swing.JDialog {
    private Prova prova;
    private List<Prova> provas;
    private ProvaDAO provaDAO;
    private boolean salvou;
    /**
     * Creates new form CartaoLocalizar
     */
    public ProvaSalvarComo(java.awt.Frame parent, boolean modal, Prova prova) {
        super(parent, modal);
        this.prova = prova;
        provas = new ArrayList<>();
        provaDAO = new ProvaDAO();
        salvou = false;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblSalvarComo = new javax.swing.JLabel();
        txfNomeProva = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        listProvas = new javax.swing.JList();
        btnAbrir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblProvasExistentes = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Salvar como");
        setResizable(false);

        lblSalvarComo.setText("Salvar como:");

        txfNomeProva.setVisible(true);

        provas = provaDAO.buscarProvas();
        listProvas.setModel(new javax.swing.AbstractListModel() {
            public int getSize() {
                return provas.size();
            }
            public Object getElementAt(int i) {
                return provas.get(i).getNome();
            }
        });
        listProvas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listProvas);

        btnAbrir.setText("Salvar");
        btnAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblProvasExistentes.setText("Provas existentes:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblSalvarComo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txfNomeProva, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAbrir))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblProvasExistentes)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblProvasExistentes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAbrir)
                    .addComponent(btnCancelar)
                    .addComponent(txfNomeProva, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSalvarComo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirActionPerformed
        boolean provaJaExiste = false;
        
        if(txfNomeProva.getText().equalsIgnoreCase("")){
            provaJaExiste = true;
        }else{
            for(Prova prova : provas){
                if(txfNomeProva.getText().equalsIgnoreCase(prova.getNome())){
                    provaJaExiste = true;
                    break;
                }
            }
        }
        if(!provaJaExiste){
            prova.setNome(txfNomeProva.getText());
            provaDAO.inserirProva(prova);
            salvou = true;
            dispose();
        }else{
            JOptionPane.showMessageDialog(this, "Já existe uma prova no banco de dados com este nome ou ele é inválido.\nEscolha um nome válido e diferente dos nomes apresentados na lista.", "Nome de prova existente", JOptionPane.YES_NO_CANCEL_OPTION);
        }
    }//GEN-LAST:event_btnAbrirActionPerformed

    public boolean salvou() {
        return salvou;
    }

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbrir;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblProvasExistentes;
    private javax.swing.JLabel lblSalvarComo;
    private javax.swing.JList listProvas;
    private javax.swing.JTextField txfNomeProva;
    // End of variables declaration//GEN-END:variables
}
